package com.ayoka.ui.course.course_estimate;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Animation;

import com.ayoka.R;
import com.ayoka.adapter.CourseEstimateQuestionMultiAdapter;
import com.ayoka.base.BaseRecyclerViewFragment;
import com.ayoka.databinding.FragmentBaseRecyclerviewBinding;
import com.ayoka.mvp.CourseEstimateMVP;
import com.ayoka.mvp.CourseEstimateMVP.CourseEstimateModel.CourseEstimateQuestionBean;
import com.ayoka.mvp.CourseEstimateQuestionMVP.CourseEstimateQuestionModel.CourseEstimateQuestionMultiEntity;
import com.ayoka.mvp.CourseEstimateQuestionMVP.CourseEstimateQuestionPresenter;
import com.ayoka.mvp.CourseEstimateQuestionMVP.ICourseEstimateQuestionView;
import com.jcp.baselibrary.brvah.BaseQuickAdapter;
import com.jcp.baselibrary.brvah.BaseViewHolder;

import java.util.List;

/**
 * @author jcp
 * @ClassName: TopicListActivity
 * @Description:
 * @date 2018-12-28 下午4:21:18
 */
public class CourseEstimateQuestionFragment extends BaseRecyclerViewFragment<CourseEstimateQuestionMultiEntity, FragmentBaseRecyclerviewBinding, CourseEstimateQuestionPresenter> implements ICourseEstimateQuestionView {

    public static CourseEstimateQuestionFragment newInstance(boolean isFirst, CourseEstimateQuestionBean courseEstimateQuestionBean, long recordId) {
        CourseEstimateQuestionFragment questionFragment = new CourseEstimateQuestionFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean("IsFirst", isFirst);
        bundle.putSerializable("CourseEstimateQuestionBean", courseEstimateQuestionBean);
        bundle.putLong("RecordId", recordId);
        questionFragment.setArguments(bundle);
        return questionFragment;
    }

    private CourseEstimateQuestionBean courseEstimateQuestionBean;
    private boolean isFirst;
    private long recordId;

    @Override
    protected void configBundleArguments() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            courseEstimateQuestionBean = (CourseEstimateQuestionBean) getArguments().getSerializable("CourseEstimateQuestionBean");
            isFirst = getArguments().getBoolean("IsFirst", true);
            recordId = getArguments().getLong("RecordId", 0);
        }
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_base_recyclerview;
    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return mDataBinding.swipeRefresh;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        mDataBinding.recyclerView.setBackgroundColor(Color.WHITE);
        return mDataBinding.recyclerView;
    }

    @Override
    public void initData() {
        mPresenter.formatList(isFirst, courseEstimateQuestionBean);
    }

    @Override
    protected CourseEstimateQuestionPresenter getPresenter() {
        return new CourseEstimateQuestionPresenter();
    }

    @Override
    protected boolean getLoadEnable() {
        return false;
    }

    @Override
    protected RecyclerView.ItemDecoration getRecycleDivider() {
        return null;
    }

    @Override
    protected BaseQuickAdapter<CourseEstimateQuestionMultiEntity, BaseViewHolder> getAdapter() {
        CourseEstimateQuestionMultiAdapter adapter = new CourseEstimateQuestionMultiAdapter();
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CourseEstimateQuestionMultiEntity multiEntity = (CourseEstimateQuestionMultiEntity) adapter.getItem(position);
                if (view.getId() == R.id.tv_option) {
                    if (multiEntity == null) return;
                    ((CourseEstimateQuestionMultiAdapter) adapter).setSingleChoice(position);
                    if (multiEntity.getItemType() == CourseEstimateQuestionMultiEntity.OPTION) {
                        CourseEstimateMVP.CourseEstimateModel.QuestionOption questionOption = (CourseEstimateMVP.CourseEstimateModel.QuestionOption) multiEntity.getData();
                        submitCourseEstimateAnswer(questionOption.Score);
                    }
                } else if (view.getId() == R.id.btn_operate) {
                    ((CourseEstimateQuestionActivity) getActivity()).showNextQuestion();
                }
            }
        });
        return adapter;
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {
    }

    @Override
    public void configCourseEstimateQuestionMultiEntityData(List<CourseEstimateQuestionMultiEntity> formatList) {
        setLoadMoreLayout(formatList, DEFAULT_PAGE_NO);
    }

    @Override
    public void onSubmitCourseEstimateAnswerCallback(boolean isSuccess) {
        if (isSuccess) {
            ((CourseEstimateQuestionActivity) getActivity()).showNextQuestion();
        }
    }

    private void submitCourseEstimateAnswer(int score) {
        mPresenter.submitCourseEstimateAnswer(courseEstimateQuestionBean.ID, courseEstimateQuestionBean.FactorGroupID, recordId, courseEstimateQuestionBean.QuestionIndex, score);
    }
}
